package com.jpyl.jpMedicalTreatment.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ProductsMessageModel;
import com.jpyl.jpMedicalTreatment.util.StringUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 产品主界面适配器
 * Created by 15697 on 2019/1/7.
 */

public class ProductAdapter extends BaseAdapter {
    private Context context;
    private List<ProductsMessageModel.ProductsModel> list;
    LayoutInflater inflater;

    public ProductAdapter(Context context, List<ProductsMessageModel.ProductsModel> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.activity_item_product, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvItemProductName.setText(list.get(i).getName());
        // TODO: 2019/4/12 产品图片取值
        StringUtil.setImageView(context, holder.imgItemProductPic, list.get(i).getPic_url(),false);
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.img_itemProduct_pic)
        ImageView imgItemProductPic;
        @BindView(R.id.tv_itemProduct_name)
        TextView tvItemProductName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
